/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBuscar.java
 *
 * Created on 20-abr-2013, 14:54:51
 */
package jumavidesktop.GUI.Mantenimientos.Usuarios.Vistas;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import jumavidesktop.GUI.Mantenimientos.Usuarios.Controlador.MUsuarios;
import jumavidesktop.Manejadores.Intefaces.INotificadoUsuario;
import jumavidesktop.Manejadores.Intefaces.ISubjectUsuario;
import jumavidesktop.Otros.Pojos.consNamed;
import jumavidesktop.Persistencia.Dao.Implementaciones.DaoUsuario;
import jumavidesktop.Persistencia.Dao.Interfaces.IDaoUsuarios;
import jumavidesktop.Persistencia.entidades.Usuario;

/**
 *
 * @author kjh
 */
public class PanelBuscarUsuario extends javax.swing.JPanel implements ISubjectUsuario{
    
    DefaultListModel modelo = new DefaultListModel();
    private ArrayList observadores;
    private Usuario useraux;
    private IDaoUsuarios daoUsuario;
    private List<Usuario> listaux;
    
    public void setLabel_Usuario(String label) {
        borrarTodo();
        String s = "Buscar por: "+label;
        this.label_Usuario.setText(s);
    }

    /** Creates new form PanelBuscar */
    public PanelBuscarUsuario() {
        initComponents();
        modelo = new DefaultListModel();
        jList1.setModel(modelo);
        observadores = new ArrayList();
        useraux = new Usuario();
        daoUsuario = new DaoUsuario();
        borrarTodo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tf_Usuario = new javax.swing.JTextField();
        label_Usuario = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        tf_Usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_UsuarioActionPerformed(evt);
            }
        });
        tf_Usuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tf_UsuarioKeyTyped(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_Usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_Usuario))
                .addContainerGap(196, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_Usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tf_Usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(249, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(63, 63, 63)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addContainerGap()))
        );
    }// </editor-fold>//GEN-END:initComponents

private void tf_UsuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_UsuarioKeyTyped
    
}//GEN-LAST:event_tf_UsuarioKeyTyped

private void tf_UsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_UsuarioActionPerformed
    consNamed consulta = new consNamed();
    String aux = label_Usuario.getText().replace("Buscar por: ", "");
    switch(aux){
        case MUsuarios.orApellido:{
            consulta.setNamedQuery(Usuario.BUSCAR_POR_APELLIDOS_LIKE);
            consulta.addParametrosLike(Usuario.PA_BUSCAR_APELLIDOS,mayuscPrimera(tf_Usuario.getText()));
            break;
        }
        case MUsuarios.orDNI:{
            consulta.setNamedQuery(Usuario.BUSCAR_POR_DNI_LIKE);
            consulta.addParametrosLike(Usuario.PA_BUSCAR_DNI,mayuscPrimera(tf_Usuario.getText()));
            break;
        }
        case MUsuarios.orNombre:{
            consulta.setNamedQuery(Usuario.BUSCAR_POR_NOMBRE_LIKE);
            consulta.addParametrosLike(Usuario.PA_BUSCAR_NOMBRE,mayuscPrimera(tf_Usuario.getText()));
            break;
        }    
    }
//    listaux.clear();
    listaux = this.daoUsuario.conNamedQuery(consulta);
    this.setUsuariosLista(listaux);
}//GEN-LAST:event_tf_UsuarioActionPerformed

private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
    int i = jList1.getSelectedIndex();
    if(i>=0){
        useraux = listaux.get(i);
        this.notificarObservadores();
    }
    updateUI();
}//GEN-LAST:event_jList1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_Usuario;
    private javax.swing.JTextField tf_Usuario;
    // End of variables declaration//GEN-END:variables
    
    private String mayuscPrimera(String c){
        String resto=c.substring(1);
        char letra = c.charAt(0);
        letra = Character.toUpperCase(letra);
        return (Character.toString(letra) + resto);
    }
    @Override
    public void registrarObserver(INotificadoUsuario observador) {
        observadores.add(observador);
    }

    @Override
    public void removerObserver(INotificadoUsuario observador) {
        int i = observadores.indexOf(observador);
        if(i>=0){
            observadores.remove(i);
        }
    }

    @Override
    public void notificarObservadores() {
        for(int i=0;i<observadores.size();i++){
            INotificadoUsuario observador = (INotificadoUsuario) observadores.get(i);
            observador.update(useraux);
        }
    }
    
    public void borrarTodo(){
        modelo.removeAllElements();
        tf_Usuario.setText("");
        this.updateUI();
        this.repaint();
    }
    
    public void repintar(){
        this.updateUI();
        this.repaint();
    }
    
    public void setUsuariosLista(List<Usuario> mod){ 
        modelo.removeAllElements();
        for(Usuario lista : mod){
            String valor = lista.getNombre()+" "+lista.getApellidos()+" con DNI: "+lista.getDni();
            this.modelo.addElement(valor);
        }
        repintar();
    }
}
